package Linklist;

import java.util.HashMap;

/**
 * @Classname JZ35复杂链表的复制
 * @Description TODO
 * @Date 2023/2/11 11:18
 * @Created by xjl
 */
public class JZ35复杂链表的复制 {

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

    HashMap<Node,Node> map=new HashMap<>();

    public Node copyRandomList(Node head) {
        if (head==null){
            return null;
        }
        if (!map.containsKey(head)){
            Node newhead=new Node(head.val);
            map.put(head,newhead);
            newhead.next=copyRandomList(head.next);
            newhead.random=copyRandomList(head.random);
        }
        return map.get(head);
    }
}
